const db = require('../db/mysql');
const Axios = require('axios');

async function main(){
    let sql_one = 'select prompt,id,status from user_prompt_url where status = 0'
    let one_res  = await db.query(sql_one)
    console.log(one_res)
    if (one_res.length == 0) {
        console.log('不存在待数据，退出')
        process.exit(1)
    }

    for (const item of one_res) {
        //发送请求
        try{
            let config = {
                method: 'post',
                url: process.env.DC_API_URL,
                headers: {
                    authorization: process.env.DC_API_AUTH,
                },
                data:{
                    "type": 2,
                    "application_id": "936929561302675456",
                    "guild_id": "1077497600221126746",
                    "channel_id": "1077497600221126749",
                    "session_id": process.env.DC_API_SID,
                    "data": {
                        "version": "1077969938624553050",
                        "id": "938956540159881230",
                        "name": "imagine",
                        "type": 1,
                        "options": [{
                            "type": 3,
                            "name": "prompt",
                            "value": item.prompt,
                        }],
                        "application_command": {
                            "id": "938956540159881230",
                            "application_id": "936929561302675456",
                            "version": "1077969938624553050",
                            "default_permission": true,
                            "default_member_permissions": null,
                            "type": 1,
                            "nsfw": false,
                            "name": "imagine",
                            "description": "Create images with Midjourney",
                            "dm_permission": true,
                            "options": [{
                                "type": 3,
                                "name": "prompt",
                                "description": "The prompt to imagine",
                                "required": true
                            }]
                        },
                        "attachments": []
                    },
                    "nonce": generateRandomNumber(20),
                },
            };
            console.log('---------axios-start--------')
            const res = await Axios(config);
            console.log('---------axios-end----------')
            if(res.data == ''){
                //处理数据
                let update_at = formatTimestampToDatetime(Date.now())
                let sql_two = 'update user_prompt_url set status = 1,update_at = "' + update_at + '" where id = ' + one_res[0].id
                let two_res  = await db.query(sql_two)
                if(two_res.changedRows == 1){
                    console.log('成功，退出')
                    process.exit(1)
                }else{
                    console.log('更新数据库失败，退出')
                    process.exit(1)
                }
            }else{
                console.log(res)
                console.log('请求失败，退出')
                process.exit(1)
            }
        }catch(error){
            console.log(error);
            console.log('报错啦，退出')
            process.exit(1)
        }
    }
}

function generateRandomNumber(n) {
    let result = '';
    for (let i = 0; i < n; i++) {
      result += Math.floor(Math.random() * 10);
    }
    return result;
}

function formatTimestampToDatetime(timestamp) {
    const date = new Date(timestamp); // 创建一个新的 Date 对象并传入时间戳
    const year = date.getFullYear();
    const month = padZero(date.getMonth() + 1); // JavaScript 中月份从 0 开始计数，所以需要加 1
    const day = padZero(date.getDate());
    const hour = padZero(date.getHours());
    const minute = padZero(date.getMinutes());
    const second = padZero(date.getSeconds());
  
    // 拼接成 MySQL DATETIME 格式的字符串并返回
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
function padZero(num) {
    return num.toString().padStart(2, '0');
}

main()
